package com.jinpeng.rabbitmqconsumer;



import com.jinpeng.rabbitmqconsumer.direct.DirectReceiver;
import org.springframework.amqp.core.AcknowledgeMode;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author JINPENG
 */
@Configuration
public class MessageListenerConfig {
    @Autowired
    private CachingConnectionFactory connectionFactory;

    @Autowired
    private DirectReceiver directReceiver;

    @Autowired
    private DirectReceiver  fanoutCarReceiver ;

    @Autowired
    private DirectReceiver  topicAllReceiver ;

    @Autowired
    private DirectReceiver  topicManReceiver ;
    @Autowired
    private DirectReceiver  topicWomenReceiver ;


    @Bean
    public SimpleMessageListenerContainer simpleMessageListenerContainer() {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory);
        /**
         *指定要创建的并发使用者数。默认值为1。
         * 建议增加并发使用者的数量， 以扩展从队列传入的消息的使用量。
         * 但是，请注意，一旦注册了多个使用者，任何订购保证都将丢失。
         * 通常，对于少量队列，请坚持使用1个使用者。
         * 不能超过maxConcurrentConsumers（如果已设置）。
         */
        container.setConcurrentConsumers(1);
        //设置消费者最大值为1
        container.setMaxConcurrentConsumers(1);
        // RabbitMQ默认是自动确认，这里改为手动确认消息
        container.setAcknowledgeMode(AcknowledgeMode.MANUAL);
        //设置消息监听类
        container.setMessageListener(directReceiver);
        container.setMessageListener(fanoutCarReceiver);
        container.setMessageListener(topicAllReceiver);
        container.setMessageListener(topicManReceiver);
        container.setMessageListener(topicWomenReceiver);

        return container;
    }

}
